import React, { useState,useContext } from "react";
import {ReducerContext} from '../../App'
import {Link} from 'react-router-dom';
import {reqLogin} from '../../request/api'

const Login=(props)=> {
  const [user,setUser]=useState({
    phone:'',
    password:''
  })
  const {dispatch,actions}=useContext(ReducerContext)

  //登录交互
  const login=()=>{
    reqLogin(user).then(res=>{
      if(res.data.code===200){
        //1.存用户信息
        dispatch(actions.changeInfo(res.data.list))
        //跳页面
        props.history.push("/index/home")
      }
    })
  }
  return (
    <div className='big'>
         <Link to='/register'>注册</Link>
        <div className='inp1'>
          手机号：
          <input
            type="text"
            value={user.phone}
            onChange={(e) => setUser({...user, phone:e.target.value}) }
          />
        </div>
        <div className='inp2'>
          密码:
          <input
            type="text"
            value={user.password}
            onChange={(e) => setUser({...user, password:e.target.value}) }
          />
        </div>
        <button onClick={() => login()}>登录</button>
      </div>
  )
}


export default Login;
